﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity;
using Runda.CMS.Common.Utility;
using System.Data;

namespace Runda.CMS.Portal.Lookup
{
    public partial class selectuser : System.Web.UI.Page
    {
        private SysUserBll sysUserBll = new SysUserBll();
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) {
                BindList("");               
            }
        }

        protected void query(object sender, EventArgs e)
        {
            BindList(userNameOrCode.Text.Trim());
        }

        private void BindList(String queryParm)
        {
            if (Request.QueryString["department"] == null)
            {
                SysUserBll userBll = new SysUserBll();
                List<SysUserEntity> userEntitys = new List<SysUserEntity>();
                if (queryParm == "")
                {
                    userEntitys = userBll.GetAll<SysUserEntity>(new List<SqlDbParameter>());
                }
                else
                {
                    userEntitys = userBll.GetAll<SysUserEntity>(" and UserName like '%" + queryParm + "%'");
                }
                this.RepeaterUsers.DataSource = userEntitys;
                this.RepeaterUsers.DataBind();
                return;
            }
            if (Request.QueryString["department"] == "maintain") {
                string departmentID = "03589d4f-d56a-4217-bb72-39c16e1e16ad";
                CommonBll bll = new CommonBll();
                string strsql = "";
                if (queryParm == "")
                {
                    strsql = "select a.UserID,UserName+ case when IsManager =1 then '-组长' else '' end +'('+c.DepartmentName +')' as UserName from Cms_Sys_User as a inner join Cms_Sys_UserDepartment as b on a.UserID=b.UserID inner join Cms_Sys_Department c on c.DepartmentID = b.DepartmentID where c.DepartmentID in ( select DepartmentID from Cms_Sys_Department where ParentID = '" + departmentID + "') or c.DepartmentID = '" + departmentID + " order by 2'";
                }
                else
                {
                    strsql = "select a.UserID,UserName+ case when IsManager =1 then '-组长' else '' end +'('+c.DepartmentName +')' as UserName from Cms_Sys_User as a inner join Cms_Sys_UserDepartment as b on a.UserID=b.UserID inner join Cms_Sys_Department c on c.DepartmentID = b.DepartmentID where (c.DepartmentID in ( select DepartmentID from Cms_Sys_Department where ParentID = '" + departmentID + "') or c.DepartmentID = '" + departmentID + "') and UserName like '%" + queryParm + "%'";
                }



                DataTable dt = bll.getDataFromSQL(strsql);
                this.RepeaterUsers.DataSource = dt;
                this.RepeaterUsers.DataBind();
            }
        }
    }
}
